# -*- coding:utf-8 -*-

import os
import sys

if os.getuid() == 0:
    pass
else:
    print("当前用户不是root用户，请用root用户执行脚本。")
    sys.exit(1)

version = input('请输入你要下载的源码包版本（2.7/3.10）')
if version == '2.7':
    url = "https://www.python.org/ftp/python/2.7/python-2.7.12-amd64.exe"
elif version == '3.10':
    url = "https://www.python.org/ftp/python/3.10.2/python-3.10.2-amd64.exe"
else:
    print("您输入的版本号有误，请重新输入！")
    sys.exit(1)

cmd = 'wget ' + url
res = os.system(cmd)
if res != 0:
    print('下载源码包失败，请检查网络')
    sys.exit(1)

if version == '2.7':
    package_name = 'python-2.7.12'
elif version == '3.10':
    package_name = 'python-3.10.2'

cmd = 'tar xf ' + package_name + '.tgz'

res = os.system(cmd)
if res != 0:
    os.system('rm' + package_name + '.tgz')
    print('解压源码包失败，请重新运行这个脚本下载源码包')
    sys.exit(1)

cmd = 'cd ' + package_name + '&& ./configure --prefix=/usr/local/python && make && make install'
res = os.system(cmd)
if res != 0:
    print("编译源码包失败，请检查是否缺少依赖库")
    sys.exit(1)